/**
 *    Copyright 2012 Geensoft S.A.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package ar.com.greensoft.neosystem.admin.users;

import java.io.Serializable;
import java.util.logging.Logger;

import javax.annotation.ManagedBean;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import ar.com.greensoft.jeecommons.web.jsf.util.JSFMenuUtil;
import ar.com.greensoft.neosystem.model.User;
import ar.com.greensoft.neosystem.services.Encoded;
import ar.com.greensoft.neosystem.services.Encoded.Algoithm;
import ar.com.greensoft.neosystem.services.PasswordEncoder;
import ar.com.greensoft.neosystem.services.ServiceException;
import ar.com.greensoft.neosystem.services.UserService;


/**
 * @author Jorge E. Villaverde
 *
 */
@RequestScoped
@ManagedBean
@Named
public class ChangePasswordBean implements Serializable {
	@Inject
	private Logger logger;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@NotNull
	@Size(min = 1, max = 50)
	private String username;
	@NotNull
	@Size(min = 1, max=50)
	private String oldPassword;
	@NotNull
	@Size(min = 1, max=50)
	private String newPassword;
	@NotNull
	@Size(min = 1, max=50)
	private String retryPassword;
	
	@EJB
	private UserService userService;
	@Inject
	@Encoded(Algoithm.HEX)
	private PasswordEncoder passwordEncoder;
	
	@PostConstruct  
	public void init() {  
	   clearData();  
	} 
	
	public String getUsername() {
		return username;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	public String getOldPassword() {
		return oldPassword;
	}
	
	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
	
	public String getNewPassword() {
		return newPassword;
	}
	
	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
	
	public String getRetryPassword() {
		return retryPassword;
	}
	
	public void setRetryPassword(String retryPassword) {
		this.retryPassword = retryPassword;
	}
	
	public void changePassword(){
		try {
			User user = userService.findUserByUsername(username);
			if(user == null){
				JSFMenuUtil.addErrorMessage("Usuario/Contraseña incorrectas");
				return;
			}
			if (!passwordEncoder.isPasswordValid(user.getPassword(), oldPassword)) {
				JSFMenuUtil.addErrorMessage("Usuario/Contraseña incorrectas");
				return;				
			}
			user.setPassword(passwordEncoder.encodePassword(newPassword));
			userService.saveUser(user);
			JSFMenuUtil.addInfoMessage("Contraseña cambiada.");
			clearData();
		} catch (ServiceException e) {
			logger.severe(e.getMessage());
		}
	}

	public void clearData() {
		this.username = null;
		this.oldPassword = null;
		this.newPassword = null;		
	}
	
	@AssertTrue(message="Las contraseñas ingresadas no coinciden")
	public boolean isPasswordMatch(){
		if(newPassword == null || retryPassword == null)
			return false;
		return newPassword.equals(retryPassword);
	}
		
}